import { gsdlist, showArea, editarea } from "../../../server/api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fieldNames: {
      text: 'pointsPlaceName',
      value: 'id',
      children: 'children',
    },
    columns: [],
    defaultIndex: 0,
    cname: "",
    show: false,
    fieldValue: "",
    cascaderValue: "",
    op:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(e) {
    this.getdata();
  },
  gsd() {
    this.setData({ show: true });
  },
  async getdata() {
    //获取详情接口
    const value = await showArea();
    if (value.code == 200) {
      const source = value.result;
      //归属地列表接口
      const res = await gsdlist();
      if (res.code == 200) {
        const data = res.result;
        this.setData({op:data})
        var aname;
        var bname;
        var cname;
        var index1;
        var index2;
        var index3;
        for (let i = 0; i < data.length; i++) {
          if (data[i].id == source[0]) {
            aname = data[i].pointsPlaceName;
            index1 = i;
            for (let j = 0; j < data[i].children.length; j++) {
              if (data[i].children[j].id == source[1]) {
                index2 = j;
                bname = data[i].children[j].pointsPlaceName;
                if (data[i].children[j].children == null) {
                  index3 = "";
                  cname = undefined;
                  data[i].children[j].children = [];
                } else {
                  for (
                    let k = 0;
                    k < data[i].children[j].children.length;
                    k++
                  ) {
                    if (data[i].children[j].children[k].id == source[2]) {
                      index3 = k;
                      cname = data[i].children[j].children[k].pointsPlaceName;
                    }
                  }
                }
              }
            }
          }
        }
        const name =
          aname +
          (bname == undefined ? "" : "-" + bname) +
          (cname == undefined ? "" : "-" + cname);
        this.setData({ cname: name });
        console.log(data[index1].childre, "///");
        const list = [
          {
            values: data,
            defaultIndex: index1,
          },
          {
            values:
              data[index1].children == undefined ? [] : data[index1].children,
            defaultIndex: index2,
          },
          {
            values:
              data[index1].children[index2] == undefined
                ? []
                : data[index1].children[index2].children,
            defaultIndex: index3,
          },
        ];
        this.setData({ columns: list });
      }
    }
  },
  onChangecascader(e)
  {
    console.log(e.detail,'e');
    const {value } = e.detail
    this.setData({cascaderValue:value})
  },
  onChange(event) {
    const { picker, value, index } = event.detail;

    if (index == 0) {
      picker.setColumnValues(
        1,
        value[0].children == null ? [] : value[0].children
      );
      picker.setColumnValues(
        2,
        value[0].children == null
          ? []
          : value[0].children[0].children == null
          ? []
          : value[0].children[0].children
      );
    } else if (index === 1) {
      picker.setColumnValues(
        2,
        value[1].children == null ? [] : value[1].children
      );
    }
  },
  onCancel() {
    // wx.navigateBack(-1)
    this.setData({ show: false,cascaderValue:'' });
  },
  async onConfirm(event) {
    console.log(this.data.cascaderValue);
    if(this.data.cascaderValue == '')
    {
      wx.showToast({
        title: '请选择归属地',
        icon:'none'
      })
    }else
    {
      const parmas = {
        adresscode:
        this.data.cascaderValue
      };
      const data = await editarea(parmas);
      if (data.code == 200) {
        wx.showToast({
          title: data.message,
          icon: "success",
          duration: 500,
        });
        this.setData({ show: false ,cascaderValue:''});
        this.getdata();
      } else {
        wx.showToast({
          title: data.message,
          icon: "error",
          duration: 500,
        });
      }
    }
    

    // return
    // const res = event.detail.value;
    // const parmas = {
    //   adresscode:
    //     res[2] == undefined && res[1] == undefined
    //       ? res[0].id
    //       : res[2] == undefined
    //       ? res[1].id
    //       : res[2].id,
    // };
    // const data = await editarea(parmas);
    // if (data.code == 200) {
    //   wx.showToast({
    //     title: data.message,
    //     icon: "success",
    //     duration: 500,
    //   });
    //   this.setData({ show: false });
    //   this.getdata();
    // } else {
    //   wx.showToast({
    //     title: data.message,
    //     icon: "error",
    //     duration: 500,
    //   });
    // }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
